
// 获取员工信息
var fuzhurenyuanchuancan=JSON.stringify({year:bzcj_currentYear,month:bzcj_currentMonth,
    pkDept:bzcj_currentDept, maps:[{[JX_BZCJ_FUZHUZHUGUAN]:JX_BZCJ_FUZHUZHUGUAN_CODE, [JX_BZCJ_FUZHURENYUAN]:JX_BZCJ_FUZHURENYUAN_CODE,[JX_BZCJ_FUZHUZUZHANG]:JX_BZCJ_FUZHUZUZHANG_CODE}]})
var bzFuZhuRenYuanALLData = commonAjax("post","/jx/bzcj/selectBzcjFuZhuRenYuan",fuzhurenyuanchuancan,"application/json;charset=utf-8");

//辅助人员页脚行变量
var bzFuZhuRenYuanTableFooter=[];
//辅助人员绩效奖金总合计的平均值：清洁工、搬运工，需要用到
var bzFuZhuRenYuanTotalResultMoney=0;
//每个表的索引
var baoZhuangCheJianFuZhuZaGongIndex=0;
var baoZhuangCheJianFuZhuRenYuanIndex=0;
var baoZhuangCheJianFuZhuZuZhangIndex=0;


var bacjFuZhuRenYuanColumns={
    fuZhuRenYuan:function (currentYM) {
        return  [
            [  {"title": '考核部门（科室）:',"align": "center", "colspan":3},
                {"field": 'id3', "title": "生产部/包装车间","align": "center", "colspan":2},
                {"title": '', "align": "center","colspan":8},
                {"title": '考核周期:',"align": "center","colspan":3},
                {"field": 'id2', "title": currentYM, "align": "center","colspan":2},
            ],
            [
                {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                {"field": 'empName', "title": '姓名', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'empCode', "title": '工号', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zuZhangName', "title": '所属组长', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zhuGuanName', "title": '所属主管', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'postName', "title": '岗位名称', "width": 30, "align": "center","hidden": "true","editor": {"type": "text"}},
                {"field": 'teamEfficiency',"title": '产线生产<br/>效率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianFuZhuRenYuanProductivity}}},
                {"field": 'accuracyImeliness',"title": '备料准确<br/>及时率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox", "options": {"onChange":baoZhuangCheJianFuZhuRenYuanProductivity}}},
                {"field": 'performanceBonusNorm', "title": '绩效奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'efficiencyBonus', "title": '生产效率奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'score5s', "title": '5s得分', "width": 30,"align": "center","editor": {"type": "textbox", "options":  {"onChange":baoZhuangCheJianFuZhuScore5s}}},
                {"field": 'penalty5s', "title": '规范化<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'penaltyBonus', "title": '质量扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'attendanceRate', "title": '出勤率<br/>(%)', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'exceptionBonus', "title": '发现异常<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'illegalFine', "title": '违规扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhu}}},
                {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'remarks', "title": '备注', "width": 130,"align": "center","editor": {"type":"textarea"}},
            ]
        ]
    },
    zuZhang:function (currentYM) {
        return  [
            [  {"title": '考核部门（科室）:',"align": "center", "colspan":3},
                {"field": 'id3', "title": "生产部/包装车间","align": "center", "colspan":2},
                {"title": '', "align": "center","colspan":8},
                {"title": '考核周期:',"align": "center","colspan":3},
                {"field": 'id2', "title": currentYM, "align": "center","colspan":2},
            ],
            [
                {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                {"field": 'empName', "title": '姓名', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'empCode', "title": '工号', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zuZhangName', "title": '所属组长', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zhuGuanName', "title": '所属主管', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'postName', "title": '岗位名称', "width": 30, "align": "center","hidden": "true","editor": {"type": "text"}},
                {"field": 'teamEfficiency',"title": '产线生产<br/>效率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox"}},
                {"field": 'accuracyImeliness',"title": '备料准确<br/>及时率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox"}},
                {"field": 'performanceBonusNorm', "title": '绩效奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'efficiencyBonus', "title": '生产效率奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'score5s', "title": '5s得分', "width": 30,"align": "center","editor": {"type": "textbox", "options":  {"onChange":baoZhuangCheJianFuZhuZuZhangScore5s}}},
                {"field": 'penalty5s', "title": '规范化<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'penaltyBonus', "title": '质量扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'attendanceRate', "title": '出勤率<br/>(%)', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'exceptionBonus', "title": '发现异常<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'illegalFine', "title": '违规扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZuZhang}}},
                {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'remarks', "title": '备注', "width": 130,"align": "center","editor": {"type":"textarea"}},
            ]
        ]
    },
    zaGong:function (currentYM) {
        return  [
            [  {"title": '考核部门（科室）:',"align": "center", "colspan":3},
                {"field": 'id3', "title": "生产部/包装车间","align": "center", "colspan":2},
                {"title": '', "align": "center","colspan":8},
                {"title": '考核周期:',"align": "center","colspan":3},
                {"field": 'id2', "title": currentYM, "align": "center","colspan":2},
            ],
            [
                {"field": 'id', "title": 'ID', "width": 0, "align": "center", "hidden": "true"},
                {"field": 'empName', "title": '姓名', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'empCode', "title": '工号', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zuZhangName', "title": '所属组长', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'zhuGuanName', "title": '所属主管', "width": 30, "align": "center","editor": {"type": "text"}},
                {"field": 'postName', "title": '岗位名称', "width": 30, "align": "center","hidden": "true","editor": {"type": "text"}},
                {"field": 'teamEfficiency',"title": '产线生产<br/>效率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox"}},
                {"field": 'accuracyImeliness',"title": '备料准确<br/>及时率(%)',"width": 30,"align": "center",
                    "editor": {"type": "textbox"}},
                {"field": 'performanceBonusNorm', "title": '绩效奖金<br/>标准', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'efficiencyBonus', "title": '生产效率奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'score5s', "title": '5s得分', "width": 30,"align": "center","editor": {"type": "textbox", "options":  {"onChange":baoZhuangCheJianFuZhuZaGongScore5s}}},
                {"field": 'penalty5s', "title": '规范化<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'penaltyBonus', "title": '质量扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'attendanceRate', "title": '出勤率<br/>(%)', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'exceptionBonus', "title": '发现异常<br/>奖金', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'illegalFine', "title": '违规扣款', "width": 30,"align": "center","editor": {"type":"textbox", "options": {"onChange":totalBaoZhuangFuZhuZaGong}}},
                {"field": 'resultMoney', "title": '综合<br/>绩效（元）', "width": 30,"align": "center","editor": {"type": "textbox", "options": {"readonly":true}}},
                {"field": 'remarks', "title": '备注', "width": 130,"align": "center","editor": {"type":"textarea"}},
            ]
        ]
    }
}

//辅助人员表格初始化
function bzFuZhuRenYuanTableInit() {
    //获取字段
    var currentYM=bzcj_currentYear+"年"+bzcj_currentMonth+"月";
    var columns = bacjFuZhuRenYuanColumns.fuZhuRenYuan(currentYM);

    //表格初始化
    $("#bzFuZhuRenYuanTable").edatagrid({
        nowrap: false,
        columns: columns,
        data:bzFuZhuRenYuanALLData.fuZhuRenYuan,
        showFooter: true,
        autoSave:true,
        fitColumns: true,
        //scrollbarSize:0,  //最右侧多出来的去除
        toolbar:"#bzFuZhuRenYuanTableToolbar",
        onBeforeEdit:function (index, row) { //编辑器打开触发事件
            baoZhuangCheJianFuZhuRenYuanIndex=index;//保存当前索引
        },
        onAfterEdit:function (index, row) {
            loadBzFuZhuFooter();
        },
        onDestroy:function (index,row) { //删除一行触发事件
            loadBzFuZhuFooter();
        }
    })
    loadBzFuZhuFooter();
}

//辅助组长表格初始化
function bzFuZhuZuZhangTableInit() {
    var currentYM=bzcj_currentYear+"年"+bzcj_currentMonth+"月";
    var columns = bacjFuZhuRenYuanColumns.zuZhang(currentYM);

    //表格初始化
    $("#bzFuZhuZuZhangTable").edatagrid({
        nowrap: false,
        columns: columns,
        showFooter: true,
        data:loadBzFuZhuZuZhangTableData(),
        autoSave:true,
        fitColumns: true,
        //scrollbarSize:0,  //最右侧多出来的去除
        toolbar:"#bzFuZhuZuZhangTableToolbar",
        onBeforeEdit:function (index, row) { //编辑器打开触发事件
            baoZhuangCheJianFuZhuZuZhangIndex=index;//保存当前索引

        },
        onAfterEdit:function (index, row) {
            loadBzFuZhuZuZhangFooter();
        },
        onDestroy:function (index,row) { //删除一行触发事件
            loadBzFuZhuZuZhangFooter();
        }
    })
    loadBzFuZhuZuZhangFooter()
}

//辅助组长表格-加载数据:
function loadBzFuZhuZuZhangTableData() {
    console.log("辅助组长表格-加载数据:")
    if (bzFuZhuRenYuanALLData){
        //封装绩效奖金标准performanceBonusNorm
        //获取辅助人员的页脚行
       // var footerRows = $('#bzFuZhuRenYuanTable').datagrid('getFooterRows');  已经保存到变量中，来回
        //获取组长的数据
        var fuZhuZuZhang = bzFuZhuRenYuanALLData.fuZhuZuZhang;
        //判断是否有值，若有证明是从已有的绩效数据库中获取的，没有则进行下面处理绩效奖金标准的问题
            //赋值绩效奖金标准performanceBonusNorm
            bzFuZhuRenYuanTableFooter.forEach(function(v){
                for (let i=0;i<fuZhuZuZhang.length;i++){
                    let z=fuZhuZuZhang[i];
                    if (v.zuZhangName==z.empName){
                        let performanceBonusNorm=fomatFloat(Number(v.resultMoney)*1.2,2)>800?800:fomatFloat(Number(v.resultMoney)*1.2,2);
                        //console.log("值：",performanceBonusNorm)
                        z.performanceBonusNorm=performanceBonusNorm;

                        z.efficiencyBonus=fomatFloat(performanceBonusNorm*0.95,2);
                        break;
                    }
                }
            })
        return fuZhuZuZhang;
    }
}
//杂工（清洁工、搬运工）表格初始化
function bzZaGongTableInit() {
    var currentYM=bzcj_currentYear+"年"+bzcj_currentMonth+"月";
    var columns = bacjFuZhuRenYuanColumns.zaGong(currentYM);

    //表格初始化
    $("#bzFuZhuZaGongTable").edatagrid({
        nowrap: false,
        columns: columns,
        data:loadBzFuZhuZaGongTableData(),
        showFooter: true,
        autoSave:true,
        fitColumns: true,
        //scrollbarSize:0,  //最右侧多出来的去除
        toolbar:"#bzFuZhuZaGongTableToolbar",
        onBeforeEdit:function (index, row) { //编辑器打开触发事件
            baoZhuangCheJianFuZhuZaGongIndex=index;//保存当前索引
        },
        onAfterEdit:function (index, row) {
            loadBzFuZhuZaGongFooter();
        },
        onDestroy:function (index,row) { //删除一行触发事件
            loadBzFuZhuZaGongFooter();
        }
    })
    loadBzFuZhuZaGongFooter()
}

//辅助杂工(清洁工、搬运工)表格-加载数据:
function loadBzFuZhuZaGongTableData() {
    if (bzFuZhuRenYuanALLData){
        //获取清洁工、搬运工的数据
        let fuZhuZaGong = bzFuZhuRenYuanALLData.fuZhuZaGong;
        if (fuZhuZaGong) {
            //绩效奖金标准：取所有辅助人员总合计的平均值：bzFuZhuRenYuanTotalResultMoney,生产效率奖金*0.95
            console.log("辅助杂工(清洁工、搬运工)表格-加载数据")
            fuZhuZaGong.forEach(function (val, index) {
                val.performanceBonusNorm = bzFuZhuRenYuanTotalResultMoney; //没有则取本页面计算好的全部辅助人员的平均值
                var efficiencyBonus=fomatFloat(bzFuZhuRenYuanTotalResultMoney * 0.95, 2); //生产效率奖金
                val.efficiencyBonus= efficiencyBonus;
              var attendanceRate=  val.attendanceRate;  //出勤率
                if(attendanceRate){
                    val.resultMoney=efficiencyBonus;
                }else {
                    val.resultMoney=efficiencyBonus*attendanceRate
                }

            })
            return fuZhuZaGong;
        }
    }
}


//获取辅助人员当前编辑行指定的jquery对象
function getEditObjectFuZhu(fieldValue) {
    var j = $('#bzFuZhuRenYuanTable').datagrid('getEditor', {
        index: baoZhuangCheJianFuZhuRenYuanIndex,
        field: fieldValue
    }).target; //编辑器对象
    return j
}

//获取辅助组长当前编辑行指定的jquery对象
function getEditObjectFuZuZuZhang(fieldValue) {
    var j = $('#bzFuZhuZuZhangTable').datagrid('getEditor', {
        index: baoZhuangCheJianFuZhuZuZhangIndex,
        field: fieldValue
    }).target; //编辑器对象
    return j
}

//获取辅助杂工(清洁工、搬运工)当前编辑行指定的jquery对象
function getEditObjectFuZuZaGong(fieldValue) {
    var j = $('#bzFuZhuZaGongTable').datagrid('getEditor', {
        index: baoZhuangCheJianFuZhuZaGongIndex,
        field: fieldValue
    }).target; //编辑器对象
    return j
}